﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using Common;

public partial class website_admin_product_brandeditor : PageBase
{
    string ImagesPath = ConfigurationManager.AppSettings["BrandImage"].ToString();

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin();

        switch (RequestBase.GetFormString("otype").ToString())
        {
            case "removefile":
                DeleteFile();
                break;
        }

        /// 保存设置
        if (RequestBase.GetFormString("___EVENTTARGET") == "save")
        {
            Save();
        }

        if (!IsPostBack)
        {
            if (RequestBase.GetQueryString("brandid") != "")
                Initialize();
        }
    } 

    #region 文件管理
    /// <summary>
    /// 匹配指定路径删除文件
    /// </summary>
    /// <param name="filePath"></param>
    void DeleteFile()
    {
        string fileOrgPath = "", filePath = "";

        fileOrgPath = Server.MapPath("../../" + ConfigurationManager.AppSettings[RequestBase.GetFormString("itemid")].ToString() + RequestBase.GetFormString("filename"));

        filePath = fileOrgPath.Replace(RequestBase.GetFormString("extname"), ".png");
        if (System.IO.File.Exists(filePath))
            System.IO.File.Delete(filePath);

        BLL.AjaxHelper.ResponseWrite("succeeded");
    }
    #endregion

    #region 数据初始化
    void Initialize()
    {
        Model.ProductBrandBase _obj = BLL.ProductBrandBase.GetObjById(int.Parse(RequestBase.GetQueryString("brandid")));
        BrandObj = _obj; 
    }

    Model.ProductBrandBase BrandObj
    {
        get
        {
            Model.ProductBrandBase _product = new Model.ProductBrandBase();

            if (!string.IsNullOrEmpty(RequestBase.GetQueryString("brandid")))
                _product.BrandId = int.Parse(RequestBase.GetQueryString("brandid"));

            _product.BrandName = txtBrandName.Value.Trim();
            _product.Description = txtComment.Text.Trim();
            _product.LineNum = int.Parse(txtLineNum.Value.Trim());
            _product.Logo = txtUploadControl_BrandImage.Value.Trim().Replace("|", "");

            return _product;
        }
        set
        {
            if (value != null)
            {
                txtBrandName.Value = value.BrandName.Trim();
                txtComment.Text = value.Description;
                txtLineNum.Value = value.LineNum.ToString();

                if (value.Logo != "")
                {
                    txtUploadControl_BrandImage.Value = "|" + value.Logo.Trim();
                    divProductImagesList_BrandImage.InnerHtml = "<div class=\"attachmentitems\">" + BLL.AttachmentsBase.GetIcoByExtName(value.Logo.Trim().Split('*')[2]) + "&nbsp;&nbsp;<a href='../../" + ImagesPath + value.Logo.Trim().Split('*')[1] + "' title='点击打开文件' target='_blank'>" + value.Logo.Trim().Split('*')[0] + "</a>&nbsp;&nbsp; <a href='javascript:void(0);' filedata='" + value.Logo.Trim() + "' itemid='TitleImage' onclick='uploadinit($(this));'>[删除]</a></div>";
                
                }
            }
        }
    } 
    #endregion

    #region 保存数据
    void Save()
    {
        string res = "";

        if (RequestBase.GetQueryString("otype").Trim() == "add")
        {
            res = BLL.ProductBrandBase.Add(BrandObj);
        }

        if (RequestBase.GetQueryString("otype").Trim() == "modify")
        {
            res = BLL.ProductBrandBase.Update(BrandObj);
        }

        switch (res.Split('|')[0])
        {
            case "succeeded":
                JScript.AlertAndReturnValueToParent("保存成功!", string.Format("window.location.href='brandmaglist.aspx?groupid={0}';", RequestBase.GetQueryString("groupid")));
                break;
            default:
                JScript.Alert(res);
                break;
        }
    }
    #endregion
}
